import 'package:flutter/material.dart';

class ZZNavigator {
  static final navKey = GlobalKey<NavigatorState>();

  static BuildContext? get rootContext => navKey.currentContext;

  /// Push 跳转
  static Future<T?> push<T extends Object?>(
      {BuildContext? context, required Widget widget}) {
    var context0 = context ?? rootContext;
    if (context0 == null) {
      return Future.error("context is null");
    }
    return Navigator.push(
      context0,
      MaterialPageRoute(builder: ((context) => widget)),
    );
  }

  /// Push 跳转 并替换成当前页
  static Future<T?> pushReplacement<T extends Object?>(
      {BuildContext? context, required Widget widget}) {
    var context0 = context ?? rootContext;
    if (context0 == null) {
      return Future.error("context is null");
    }
    return Navigator.pushReplacement(
      context0,
      MaterialPageRoute(builder: ((context) => widget)),
    );
  }

  /// Push 跳转 清空前面的页面
  static Future<T?> pushAndRemoveUntil<T extends Object?>(
      {BuildContext? context, required Widget widget}) {
    var context0 = context ?? rootContext;
    if (context0 == null) {
      return Future.error("context is null");
    }
    return Navigator.pushAndRemoveUntil(
      context0,
      MaterialPageRoute(builder: ((context) => widget)),
      (route) => false,
    );
  }

  /// Present 跳转
  static Future<T?> present<T extends Object?>(
      {BuildContext? context, required Widget widget, bool animation = true}) {
    var context0 = context ?? rootContext;
    if (context0 == null) {
      return Future.error("context is null");
    }
    return animation
        ? Navigator.of(context0).push(MaterialPageRoute(
            // maintainState: true,
            fullscreenDialog: true,
            builder: (context) => widget))
        : Navigator.push(
            context0,
            PageRouteBuilder(
              transitionDuration: const Duration(milliseconds: 0),
              pageBuilder: (BuildContext context, Animation<double> animation,
                  Animation secondaryAnimation) {
                return FadeTransition(
                  //使用渐隐渐入过渡,
                  opacity: animation,
                  child: widget, //路由B
                );
              },
            ),
          );
  }

  /// Present 跳转 清空前面的页面
  static Future<T?> presentAndRemoveUntil<T extends Object?>(
      {BuildContext? context, required Widget widget, bool animation = true}) {
    var context0 = context ?? rootContext;
    if (context0 == null) {
      return Future.error("context is null");
    }
    return animation
        ? Navigator.of(context0).pushAndRemoveUntil(
            MaterialPageRoute(
                // maintainState: true,
                fullscreenDialog: true,
                builder: (context) => widget),
            (route) => false,
          )
        : Navigator.pushAndRemoveUntil(
            context0,
            PageRouteBuilder(
              transitionDuration: const Duration(milliseconds: 0),
              pageBuilder: (BuildContext context, Animation<double> animation,
                  Animation secondaryAnimation) {
                return FadeTransition(
                  //使用渐隐渐入过渡,
                  opacity: animation,
                  child: widget, //路由B
                );
              },
            ),
            (route) => false,
          );
  }

  /// Pop 跳转 清空前面的页面
  static pop<T extends Object?>({BuildContext? context, T? result}) {
    var context0 = context ?? rootContext;
    if (context0 == null) {
      return Future.error("context is null");
    }
    Navigator.of(context0).pop(result);
  }
}
